﻿package engine.gui 
{
	import engine.managers.AssetManager;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.events.Event;

	
	/**
	 * ...
	 * @author Jon Smelquist
	 */
	public class ChatBox extends Sprite
	{
		[Embed(source='/assets/gui/chatBox.png')]
		protected static const CHAT_BOX:Class;
		protected var _inputBox:TextField;
		protected var _textBox:TextField;
		
		public function getText():String
		{
			var txt:String = _inputBox.text;
			_inputBox.text = "";
			return txt;
		}
		
		public function ChatBox() 
		{
			this.x = 0;
			this.y = 480;
			var background:Shape = new Shape();
			var source:Bitmap = new CHAT_BOX();
			var bitMapData:BitmapData = new BitmapData( source.width, source.height, true, 0x00000000 );
			bitMapData.draw(source);
			background.graphics.beginBitmapFill(bitMapData);
			background.graphics.drawRect( 0, 0, 640, 120);
			addChild(background);
			
			buildInputBox();
			buildTextBox();
		}
		
		private function buildInputBox():void
		{
			_inputBox = new TextField();
			_inputBox.height = 20;
			_inputBox.width = 640;
			_inputBox.y = 100;
			_inputBox.x = 0;
			_inputBox.text = "Enter commands here...";
			
			_inputBox.type = TextFieldType.INPUT;

            var format:TextFormat = new TextFormat();
            format.font = "Verdana";
            format.color = 0xFF0000;
            format.size = 10;
            format.underline = true;
			
			_inputBox.defaultTextFormat = format;

			addChild(_inputBox);
		}
		
		private function buildTextBox():void
		{
			_textBox = new TextField();
			_textBox.height = 98;
			_textBox.width = 640;
			_textBox.y = 2;
			_textBox.x = 0;

            var format:TextFormat = new TextFormat();
            format.font = "Verdana";
            format.color = 0xFF0000;
            format.size = 10;
            format.underline = true;
			
			_textBox.defaultTextFormat = format;

			addChild(_textBox);
		}
		
		public function appendLine(line:String):void
		{	
			line =  "\n" + line;
			_textBox.appendText(line);
			_textBox.scrollV = _textBox.scrollV + 10;
		}
	}
	
}